﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using Common;
using Model;
using PositionServer.Service;

namespace AcceptServer.Service
{
    class PhoneUserProtocol
    {
       
        OperateDataBase operateDataBase = new OperateDataBase();
         
        public byte[] PhoneUserRegister(byte[] realData, IPAddress clientIp)
        {
            byte[] phoneNumberbyts = new byte[6];
            Array.Copy(realData, 5, phoneNumberbyts, 0, 6);
            byte[] serialNumber = new byte[2];
            Array.Copy(realData, 11, serialNumber, 0, 2);
 
            //0-7  IMEI 8byte
            byte[] IMEI_bytes = new byte[8];
            Array.Copy(realData, ConstParameters.MBodySatrtIndex, IMEI_bytes, 0, 8);
            string IMEI = CommonRoutine.ByteToBcdString(IMEI_bytes);
            IMEI = IMEI.Remove(0, 1);

            //8-11  4byte  Version
            byte[] version_bytes = new byte[4];
            Array.Copy(realData, ConstParameters.MBodySatrtIndex + 8, version_bytes, 0, 4);
            Array.Reverse(version_bytes);
            int nowVersion = BitConverter.ToInt32(version_bytes, 0);
              
            //------------------------将手机用户登陆录入数据库
            string phoneNum = OperateAgreement.GetPhoneNumInMessageHead(realData);

            int result = 0;
            if (phoneNum.StartsWith("177"))
            {
                result = 0;//未注册用户
            }
            else
            {
                PhoneUserInfo model = operateDataBase.PhoneUserGetModelByNumber(phoneNum);
                if (model != null)
                {
                    model.LastLoginTime = DateTime.Now;                   
                    //========================IMEI未绑定,进行绑定
                    if (string.IsNullOrEmpty(model.Imei))                    
                    {
                        model.Imei = IMEI;
                        if (!operateDataBase.PhoneUserUpdateWhenLogin(model))
                        {
                            Console.WriteLine("手机客户登陆记录更新失败："
                                + phoneNum + DateTime.Now);
                        }
                        result = 1;
                    }
                    else
                    {
                        //上传IMEI与数据库中的IMEI不匹配
                        if (IMEI != model.Imei)
                        {
                            result = 3;
                        }
                        else
                        {
                            result = 2;
                        }
                    }

                }
                else  //数据库无记录  
                {
                    result = 4;
                    //暂时自动加入数据库中
                    PositionService positionService = new PositionService();
                    PhoneUserInfo newModel = new PhoneUserInfo();
                    newModel.RegistTime = DateTime.Now;
                    newModel.LastLoginTime = DateTime.Now;
                    newModel.LoginCount = 1;
                    newModel.Name = "";
                    newModel.Imei = IMEI;
                    newModel.PhoneNumber = phoneNum;

                    positionService.PhoneUserAdd(newModel);

                }   
            }            
            Console.WriteLine("登录结果：========"+result+"=========");
            List<byte> body = new List<byte>();
            try
            {

            //    switch (result)
            //    {
            //        case 0://未注册的用户
            //            body.Add(0);
            //            break;
            //        case 1: //Imei首次绑定
            //            body.Add(1);
            //            break;
            //        case 2://验证正常通过
            //            //------发送订单列表
                        #region  发送给用户

                        List<OrderInfo> orderList = new List<OrderInfo>();
                        
                        orderList.AddRange(AcceptMainForm.OrderList.Values.Cast<OrderInfo>().
                        Where(order => order.PhoneNumber == phoneNum && order.PsgerAcceptTime < DateTime.Now.AddDays(-7) &&
                            (order.State == OrderState.S4_OrderPaid || order.State == OrderState.S5_SentInfo ||
                            order.State== OrderState.S6_AcceptByDriver)
                            
                        ));
                        if (orderList.Count>0)
                        {
                            List<byte> msgbody = new List<byte>();
                            //--------orderCount 1byte
                            msgbody.Add((byte)orderList.Count);
                            foreach (OrderInfo order in orderList)
                            {
                                //--------orderId  4byte
                                msgbody.AddRange(CommonRoutine.BcdStringToBytes(order.PhoneOrderId));
                                //--------carPhoneNumber 6byte
                                string carphonestr = order.CarPhoneNumber;
                                byte[] carphoneBcd = CommonRoutine.BcdStringToBytes(
                                    carphonestr.PadLeft(12, '0'));
                                msgbody.AddRange(carphoneBcd);
                                //--------driverPhoneNumber 6byte
                                string driverphonestr = order.DriverPhoneNumber;
                                byte[] driverphoneBcd = CommonRoutine.BcdStringToBytes(
                                    driverphonestr.PadLeft(12, '0'));
                                msgbody.AddRange(driverphoneBcd);
                                //--------price 2byte
                                msgbody.AddRange(BitConverter.GetBytes(order.Price));

                            }
                           
                            byte[] msgHead = OperateAgreement.GetMessageHead(
                                ConstParameters.CmdPhoneUserSendOrderInfo,
                                false, false, msgbody.Count, phoneNumberbyts,
                                                                new byte[] { 0, 0 }, 0, 0);
                            byte[] tempMsg = OperateAgreement.PackageSendData(
                                msgHead, msgbody.ToArray());
                            IPEndPoint phoneUserIp = 
                            AcceptMainForm.IpEndPointList[phoneNum] as IPEndPoint;
                            if (phoneUserIp!=null)
                            {
                                AcceptMainForm.SendDataToIpAddress(
                                    tempMsg, phoneUserIp, phoneNum);          
                            }                            
                        }
                                                                      
                        #endregion

                //        body.Add(1);
                //        break;
                //    case 3://本次Imei与绑定的IMEI不符
                //        body.Add(1);
                //        break;
                //    case 4://数据库中无此号码
                //        body.Add(3);
                //        break;

                //}                             
                string phonestr = OperateConfig.GetCenterPhoneNumber();
                byte[] phoneBcd = CommonRoutine.BcdStringToBytes(phonestr.PadLeft(12, '0'));
                body.AddRange(phoneBcd);                                 
                string version = AcceptMainForm.PhoneUserClientVersion;
                byte[] vBytes = Encoding.GetEncoding("GBK").GetBytes(version);
                body.AddRange(vBytes);
  
            }
            catch (Exception ex)
            {
                CommonRoutine.HandlingExceptions(ex);
                LogService.WriteLog("PhoneUserProtocol.cs-Register");
                body.Clear();
                body.Add(0);
            }

            byte[] messageHead = OperateAgreement.GetMessageHead(
                ConstParameters.CmdPhoneUserLoginReply,
                false, false, body.Count, phoneNumberbyts,
                                                serialNumber, 0, 0);
            byte[] tempData = OperateAgreement.PackageSendData(messageHead, body.ToArray());



            return tempData;
        }

        public byte[] PhoneUserRegistRequest(byte[] realData)
        {
            byte[] phoneNumberbyts = new byte[6];
            Array.Copy(realData, 5, phoneNumberbyts, 0, 6);
            byte[] serialNumber = new byte[2];
            Array.Copy(realData, 11, serialNumber, 0, 2);

            string phoneNumber = OperateAgreement.GetPhoneNumInMessageHead(realData);
            Random ram = new Random();
            int intRandom = ram.Next(0, 999999);
            string strRandom = intRandom.ToString("000000");
            AcceptMainForm.PrivateCarRegistCodeList[phoneNumber] =
            strRandom + "|" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");


            //发短信phoneNumber 
            string smsContent = "感谢您注册招车王用户,您的验证码为【";
            smsContent += intRandom.ToString("000000");
            smsContent += "】,请在限定时间内在软件中输入该验证码！";
            string result = UnicomSMS.SendSMS(phoneNumber, smsContent);
            byte resultByte = 0;
            if (result.Contains("发送短信成功"))
            {
                resultByte = 1;
            }
            byte body = resultByte;
            byte[] head = OperateAgreement.GetMessageHead(
                ConstParameters.CmdPhoneUserRegistRequestReply,
                false, false, 1, phoneNumberbyts, serialNumber, 0, 0);
            List<byte> message = new List<byte>();
            message.AddRange(head);
            message.Add(body);
            return message.ToArray();
        }


        public byte[] PhoneUserVerifyRegistCode(byte[] realData)
        {
            byte[] phoneNumberbyts = new byte[6];
            Array.Copy(realData, 5, phoneNumberbyts, 0, 6);
            byte[] serialNumber = new byte[2];
            Array.Copy(realData, 11, serialNumber, 0, 2);

            string phoneNumber = OperateAgreement.GetPhoneNumInMessageHead(realData);

            //从消息体中取出验证码 0-3
            byte[] registCodeBytes = new byte[3];
            Array.Copy(realData, ConstParameters.MBodySatrtIndex, registCodeBytes, 0, 3);
            string registcode = CommonRoutine.ByteToBcdString(registCodeBytes);

            List<byte> body = new List<byte>();
            bool isExist = operateDataBase.PhoneUserExists(phoneNumber);
            if (isExist)
            {
               // body.Add(5);//该号码已注册
                
                //已存在的用户清空其IMEI
                operateDataBase.PhoneUserClearImei(phoneNumber);
            }
            //else
            //{
                if (AcceptMainForm.PrivateCarRegistCodeList.Contains(phoneNumber) ||
                        registcode == DateTime.Now.AddDays(1).ToString("yyMMdd")
                        || registcode == "010203")
                {
                    string[] verifyInfo = null;
                    if (AcceptMainForm.PrivateCarRegistCodeList.Contains(phoneNumber))
                    {
                        string value = AcceptMainForm.PrivateCarRegistCodeList
                        [phoneNumber].ToString();
                        verifyInfo = value.Split('|');
                    }

                    if ((verifyInfo != null && verifyInfo[0] == registcode)
                        || registcode == DateTime.Now.AddDays(1).ToString("yyMMdd")
                        || registcode == "010203")
                    {
                        TimeSpan span = new TimeSpan();
                        if (verifyInfo != null)
                        {
                            span = DateTime.Now - DateTime.Parse(verifyInfo[1]);
                        }

                        if (verifyInfo != null && span.Seconds > 100)
                        {
                            body.Add(2);    //表示验证码正确但已超时                                                  
                        }
                        else//验证成功
                        {
                            body.Add(1); //表示注册成功  可尝试登录  

                            if (!isExist)
                            {
                                //加入数据库中
                                PhoneUserInfo phoneUser = new PhoneUserInfo();
                                phoneUser.PhoneNumber = phoneNumber;
                                phoneUser.LoginCount = 1;
                                phoneUser.RegistTime = DateTime.Now;
                                phoneUser.LastLoginTime = DateTime.Now;
                                operateDataBase.PhoneUserAdd(phoneUser);    
                            }                            
                        }
                    }
                    else
                    {
                        body.Add(3);//表示验证码错误
                    }
                }
                else
                {
                    body.Add(4);//表示该号码未请求过注册
                }
           // }

            byte[] messageHead = OperateAgreement.GetMessageHead(
                ConstParameters.CmdPhoneUserVerifyRegistCodeReply,
                false, false, body.Count, new byte[6], new byte[2], 0, 0);
            byte[] tempData = OperateAgreement.PackageSendData(messageHead,
                body.ToArray());
            return tempData;
        }

        public byte[] PhoneUserSubmitOrder(byte[] realData)
        {
            byte[] phoneNumberbyts = new byte[6];
            Array.Copy(realData, 5, phoneNumberbyts, 0, 6);
            byte[] serialNumber = new byte[2];
            Array.Copy(realData, 11, serialNumber, 0, 2);

            string phoneNumber = OperateAgreement.GetPhoneNumInMessageHead(realData);
             
            //0-4  StartLng 5byte
            byte[] startLngbytes = new byte[5];
            Array.Copy(realData, ConstParameters.MBodySatrtIndex, startLngbytes, 0, 5);
            double slng = 0;
            double.TryParse(CommonRoutine.ByteToBcdString(startLngbytes), out slng);
            //5-9  StartLat 5byte
            byte[] startLatbytes = new byte[5];
            Array.Copy(realData, ConstParameters.MBodySatrtIndex+5, startLatbytes, 0, 5);
            double slat = 0;
            double.TryParse(CommonRoutine.ByteToBcdString(startLatbytes), out slat);
            //10-14  EndtLng 5byte
            byte[] endLngbytes = new byte[5];
            Array.Copy(realData, ConstParameters.MBodySatrtIndex+10, endLngbytes, 0, 5);
            double elng = 0;
            double.TryParse(CommonRoutine.ByteToBcdString(endLngbytes), out elng);
            //15-19  EndLat 5byte
            byte[] endLatbytes = new byte[5];
            Array.Copy(realData, ConstParameters.MBodySatrtIndex + 15, endLatbytes, 0, 5);
            double elat = 0;
            double.TryParse(CommonRoutine.ByteToBcdString(endLatbytes), out elat);

            slat = slat / 1000000;
            slng = slng / 1000000;
            elat = elat / 1000000;
            elng = elng / 1000000;

            //20-25  stime 6byte
            byte[] startTimebytes = new byte[6];
            Array.Copy(realData, ConstParameters.MBodySatrtIndex + 20, startTimebytes, 0, 6);
            DateTime sTime;
            string strStartTime = CommonRoutine.ByteToBcdString(startTimebytes);
            string strSt = strStartTime.Substring(0, 4) + "-" +
                           strStartTime.Substring(4, 2) + "-" +
                           strStartTime.Substring(6, 2) + " " +
                           strStartTime.Substring(8, 2) + ":" +
                           strStartTime.Substring(10, 2) + ":" + "00";

            DateTime.TryParse(strSt, out sTime);
            //26-31  etime 6byte
            byte[] endTimebytes = new byte[6];
            Array.Copy(realData, ConstParameters.MBodySatrtIndex + 26, endTimebytes, 0, 6);
            DateTime eTime;
            String strEndTime = CommonRoutine.ByteToBcdString(endTimebytes);
            string strEt = strEndTime.Substring(0, 4) + "-" +
                           strEndTime.Substring(4, 2) + "-" +
                           strEndTime.Substring(6, 2) + " " +
                           strEndTime.Substring(8, 2) + ":" +
                           strEndTime.Substring(10, 2) + ":" + "00";
            DateTime.TryParse(strEt, out eTime);
            //32-35  orderId  4byte
            byte[] orderidebytes = new byte[4];
            Array.Copy(realData, ConstParameters.MBodySatrtIndex + 32, orderidebytes, 0, 4);
            string orderId = CommonRoutine.ByteToBcdString(orderidebytes);
            //36  startAddressLength 
            byte sdLenght = realData[ConstParameters.MBodySatrtIndex + 36];
            //37-37+startAddressLength
            string strStartAddress = ASCIIEncoding.GetEncoding("GBK").GetString(
                realData, ConstParameters.MBodySatrtIndex + 37, (int) sdLenght);
            //37+startAddressLength+1
            byte edLenght = realData[ConstParameters.MBodySatrtIndex +
                37+sdLenght];
            string strEndAddress = ASCIIEncoding.GetEncoding("GBK").GetString(
                realData, ConstParameters.MBodySatrtIndex +
                37 + sdLenght + 1, (int)edLenght);
            ////36-37  price 2byte
            //byte[] priceBytes = new byte[2];
            //Array.Copy(realData,ConstParameters.MBodySatrtIndex+36,priceBytes,0,2);
            //short price = BitConverter.ToInt16(priceBytes,0);

            PhoneUserInfo phoneUser = operateDataBase.PhoneUserGetModelByNumber(phoneNumber);
            //if (phoneUser == null)
            //{
            //    orderidebytes = new byte[]{0,0,0,0};
            //}
            //else
            //{
                bool existOrderId = operateDataBase.OrderExistsOrderId(phoneNumber, orderId);
                if (!existOrderId)
                {
                    OrderInfo order = new OrderInfo();
                    order.PhoneNumber = phoneNumber;
                    order.StartLng = slng;
                    order.StartLat = slat;
                    order.EndLng = elng;
                    order.EndLat = elat;
                    order.StartAddress = strStartAddress;
                    order.EndAddress = strEndAddress;
                    order.AgreeStartTime = sTime;
                    order.AgreeEndTime = eTime;
                    order.CallTime = DateTime.Now;
                    order.State = OrderState.S1_NewOrder;
                    order.PhoneOrderId = orderId; 
                    if (phoneUser != null && phoneUser.Name != null)
                    {
                        order.PassengerName = phoneUser.Name;   
                    }                     
                    order.PassengerName = "";
                    int id = operateDataBase.OrderAdd(order);
                    if (id <= 0)
                    {
                        id = 0;
                    }
                    else
                    {
                        order.Id = id;
                        AcceptMainForm.OrderList.Add(id, order);
                    }
                //}
            }         
            
            byte[] body = orderidebytes;
            byte[] head = OperateAgreement.GetMessageHead(
                ConstParameters.CmdPhoneUserSubmitOrderReply,
                false, false, body.Length, phoneNumberbyts, serialNumber, 0, 0);

            byte[] tempData = OperateAgreement.PackageSendData(head,
               body.ToArray());
            return tempData;
           
        }

        public byte[] PhoneUserAcceptOrder(byte[] realData)
        {
            byte[] phoneNumberbyts = new byte[6];
            Array.Copy(realData, 5, phoneNumberbyts, 0, 6);
            byte[] serialNumber = new byte[2];
            Array.Copy(realData, 11, serialNumber, 0, 2);

            string phoneNumber = OperateAgreement.GetPhoneNumInMessageHead(realData);
            //0-4  StartLng 5byte
            byte[] orderIdbytes = new byte[4];
            Array.Copy(realData, ConstParameters.MBodySatrtIndex, orderIdbytes, 0, 4);
            string phoneOrderId = CommonRoutine.ByteToBcdString(orderIdbytes);
            DateTime timeNow = DateTime.Now;
            lock (AcceptMainForm.OrderList.SyncRoot)
            {
                foreach (OrderInfo order in AcceptMainForm.OrderList.Values)
                {
                    if (order.PhoneNumber==phoneNumber && 
                        order.PhoneOrderId==phoneOrderId)
                    {
                        order.PsgerAcceptTime = timeNow;                        
                    }
                }
            }
            bool updateSucceed = operateDataBase.OrderUpdateWhenPsgAcceptOrder(
            phoneNumber, phoneOrderId, timeNow.ToString("yyyy-MM-dd HH:mm:ss"));
            if (!updateSucceed)
            {
                return new byte[]{0};
            }
            byte[] body = orderIdbytes;
            byte[] head = OperateAgreement.GetMessageHead(
                ConstParameters.CmdPhoneUserAcceptOrderReply,
                false, false, body.Length, phoneNumberbyts, serialNumber, 0, 0);
            byte[] tempData = OperateAgreement.PackageSendData(head,
               body.ToArray());
            return tempData;
        }

        public byte[] PhoneUserCancleOrder(byte[] realData)
        {
            byte[] phoneNumberbyts = new byte[6];
            Array.Copy(realData, 5, phoneNumberbyts, 0, 6);
            byte[] serialNumber = new byte[2];
            Array.Copy(realData, 11, serialNumber, 0, 2);

            string phoneNumber = OperateAgreement.GetPhoneNumInMessageHead(realData);
            //0-4  orderId 5byte
            byte[] orderIdbytes = new byte[4];
            Array.Copy(realData, ConstParameters.MBodySatrtIndex, orderIdbytes, 0, 4);
            string phoneOrderId = CommonRoutine.ByteToBcdString(orderIdbytes);
            DateTime timeNow = DateTime.Now;
            lock (AcceptMainForm.OrderList.SyncRoot)
            {
                foreach (OrderInfo order in AcceptMainForm.OrderList.Values)
                {
                    if (order.PhoneNumber == phoneNumber &&
                        order.PhoneOrderId == phoneOrderId)
                    {
                        order.State = OrderState.S0_Cancled;
                    }
                }
            }
            bool updateSucceed = operateDataBase.OrderCancleOrder(
            phoneNumber,phoneOrderId);
            if (!updateSucceed)
            {
                return new byte[] { 0 };
            }
            byte[] body = orderIdbytes;
            byte[] head = OperateAgreement.GetMessageHead(
                ConstParameters.CmdPhoneUserCancleOrderReply,
                false, false, body.Length, phoneNumberbyts, serialNumber, 0, 0);
            byte[] tempData = OperateAgreement.PackageSendData(head,
                body.ToArray());
            return tempData;
        }

        public byte[] PhoneUserPaidAnOrder(byte[] realData)
        {
            byte[] phoneNumberbyts = new byte[6];
            Array.Copy(realData, 5, phoneNumberbyts, 0, 6);
            byte[] serialNumber = new byte[2];
            Array.Copy(realData, 11, serialNumber, 0, 2);

            string phoneNumber = OperateAgreement.GetPhoneNumInMessageHead(realData);
            //0-3  orderId 4byte
            byte[] orderIdbytes = new byte[4];
            Array.Copy(realData, ConstParameters.MBodySatrtIndex, orderIdbytes, 0, 4);
            string phoneOrderId = CommonRoutine.ByteToBcdString(orderIdbytes);
            //4 paytype 1byte
            byte paytype = realData[ConstParameters.MBodySatrtIndex + 4];
            // payorderId 
            string payorderId = ASCIIEncoding.ASCII.GetString(realData,
            ConstParameters.MBodySatrtIndex + 5, 
            realData.Length - 19 -5);
            DateTime timeNow = DateTime.Now;

            bool updateSucceed = operateDataBase.OrderUpdateWhenPaid(
            phoneNumber, phoneOrderId,(int)paytype,payorderId);
            if (!updateSucceed)
            {
                return new byte[] { 0 };
            }


            OrderInfo order = null;
            lock (AcceptMainForm.OrderList.SyncRoot)
            {                
                foreach (OrderInfo orderInfo in AcceptMainForm.OrderList.Values)
                {
                    if (orderInfo.PhoneNumber == phoneNumber &&
                        orderInfo.PhoneOrderId == phoneOrderId)
                    {
                        orderInfo.State = OrderState.S4_OrderPaid;
                        order = orderInfo;
                    }
                }
            }

            if (order!=null)
            {
                #region 发送给车载端
                IPEndPoint taxiIp = null;
                lock (AcceptMainForm.TaxiList.SyncRoot)
                {
                    TaxiInfo taxi = AcceptMainForm.TaxiList[order.CarPhoneNumber] as TaxiInfo;
                    if (taxi != null && taxi.DriverCardId == order.DriverCardId)
                    {
                        taxiIp = AcceptMainForm.IpEndPointList[order.CarPhoneNumber] as IPEndPoint;
                    }
                }
                if (taxiIp != null)
                {
                    List<byte> body2 = new List<byte>();
                    string strCardId = order.DriverCardId;
                    byte[] cardIdbytes = new byte[10];
                    byte[] tempbytes = Encoding.GetEncoding("GBK").GetBytes(strCardId);
                    for (int k = 0; k < tempbytes.Length; k++)
                    {
                        if (k < tempbytes.Length)
                        {
                            cardIdbytes[k] = tempbytes[k];
                        }
                        else
                        {
                            cardIdbytes[k] = 32;
                        }
                    }
                    //--------cardId  10byte
                    body2.AddRange(cardIdbytes);
                    //--------orderId  4byte                
                    byte[] idBytes = BitConverter.GetBytes(order.Id);
                    Array.Reverse(idBytes);
                    body2.AddRange(idBytes);
                    //--------psgPhoneNumber 6byte                                
                    body2.AddRange(phoneNumberbyts);
                    //----nameBytes
                    byte[] namebytes = new byte[10];
                    byte[] tempNamebytes = Encoding.GetEncoding("GBK").GetBytes(order.PassengerName);
                    for (int k = 0; k < tempNamebytes.Length; k++)
                    {
                        if (k < tempbytes.Length)
                        {
                            namebytes[k] = tempNamebytes[k];
                        }
                        else
                        {
                            namebytes[k] = 32;
                        }
                    }
                    body2.AddRange(namebytes);

                    //--------startLng 5byte               
                    string startLngstr =
                        ((order.StartLng * Math.Pow(10, 6)).ToString().PadLeft(10, '0'));
                    byte[] lstartLngBytes = CommonRoutine.BcdStringToBytes(startLngstr);
                    body2.AddRange(lstartLngBytes);
                    //--------startLat 5byte               
                    string startLatstr =
                        ((order.StartLat * Math.Pow(10, 6)).ToString().PadLeft(10, '0'));
                    byte[] startLatBytes = CommonRoutine.BcdStringToBytes(startLatstr);
                    body2.AddRange(startLatBytes);
                    //--------endLng 5byte               
                    string endLngstr =
                        ((order.EndLng * Math.Pow(10, 6)).ToString().PadLeft(10, '0'));
                    byte[] endLngBytes = CommonRoutine.BcdStringToBytes(endLngstr);
                    body2.AddRange(endLngBytes);
                    //--------endLat 5byte               
                    string endLatstr =
                        ((order.EndLat * Math.Pow(10, 6)).ToString().PadLeft(10, '0'));
                    byte[] endLatBytes = CommonRoutine.BcdStringToBytes(endLatstr);
                    body2.AddRange(endLatBytes);
                    //--------agreeTime 7byte
                    string agreeTime = order.AgreeStartTime.ToString("yyyyMMddHHmmss");
                    byte[] agreeTimeBytes = CommonRoutine.BcdStringToBytes(agreeTime);
                    body2.AddRange(agreeTimeBytes);
                    //----------startAddressByteLength  1byte
                    byte[] btStartAddress = ASCIIEncoding.GetEncoding("GBK").GetBytes(
                        order.StartAddress);
                    body2.Add((byte)btStartAddress.Length);
                    //----------startAddrsss  startAddressByteLength byte
                    body2.AddRange(btStartAddress);
                    //----------endAddrsssByteLength  1byte
                    byte[] btEndAddress = ASCIIEncoding.GetEncoding("GBK").GetBytes(
                        order.EndAddress);
                    body2.Add((byte)btStartAddress.Length);
                    //----------endAddress   endAddrsssByteLength byte
                    body2.AddRange(btEndAddress);

                    byte[] messageHead2 = OperateAgreement.GetMessageHead(
                        ConstParameters.CmdTaxiSendAnOrderInfo,
                        false, false, body2.Count, phoneNumberbyts,
                                                        new byte[] { 0, 0 }, 0, 0);
                    byte[] tempData2 = OperateAgreement.PackageSendData(messageHead2, body2.ToArray());
                    AcceptMainForm.SendDataToIpAddress(
                    tempData2, taxiIp, order.CarPhoneNumber);
                }
                #endregion  
            }
            
        
            byte[] body = orderIdbytes;
            byte[] head = OperateAgreement.GetMessageHead(
                ConstParameters.CmdPhoneUserPaidAnOrderReply,
                false, false, body.Length, phoneNumberbyts, serialNumber, 0, 0);
            byte[] tempData = OperateAgreement.PackageSendData(head,
                body.ToArray());
            return tempData;
        }


        public byte[] PhoneUserSetInfo(byte[] realData)
        {
            byte[] phoneNumberbyts = new byte[6];
            Array.Copy(realData, 5, phoneNumberbyts, 0, 6);
            byte[] serialNumber = new byte[2];
            Array.Copy(realData, 11, serialNumber, 0, 2);

            string phoneNumber = OperateAgreement.GetPhoneNumInMessageHead(realData);
            //0-10  Name 10byte
            string strName = ASCIIEncoding.GetEncoding("GBK").GetString(
                realData, ConstParameters.MBodySatrtIndex, 10);

            bool blResult = operateDataBase.PhoneUserUpdateInfoByUser(phoneNumber,
                                                                    strName);
            byte byteResult = blResult ? (byte)1 : (byte)0;
            if (blResult)
            {
                lock (AcceptMainForm.OrderList.SyncRoot)
                {
                    foreach (OrderInfo order in AcceptMainForm.OrderList.Values)
                    {
                        if (order.PhoneNumber == phoneNumber)
                        {
                            order.PassengerName = strName;
                        }
                    }
                }
            }
            byte[] body = new byte[] { byteResult };
            byte[] head = OperateAgreement.GetMessageHead(
                ConstParameters.CmdPhoneUserUpdateInfoReply,
                false, false, body.Length, phoneNumberbyts, serialNumber, 0, 0);
            byte[] tempData = OperateAgreement.PackageSendData(head,
                body.ToArray());
            return tempData;
        }

    }
}
